/** Copyright 2020-2023 Alibaba Group Holding Limited.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef MODULES_BASIC_DS_ARRAY_VINEYARD_MOD_
#define MODULES_BASIC_DS_ARRAY_VINEYARD_MOD_

#include <memory>

#include "client/client.h"
#include "client/ds/blob.h"
#include "client/ds/i_object.h"

namespace vineyard {

template <typename T>
class ArrayBaseBuilder;

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif

/**
 * @brief The array type in vineyard.
 *
 * @tparam T The type for the elements.
 */
template <typename T>
class [[vineyard]] Array : public Registered<Array<T>> {
 public:
  /**
   * @brief Get the element at the given location.
   *
   * @param loc The given location to get the element.
   */
  [[shared]] const T& operator[](size_t loc) const { return data()[loc]; }

  /**
   * @brief Get the size of the array.
   *
   * @return The size.
   */
  [[shared]] size_t size() const { return size_; }

  /**
   * @brief Get the pointer to the beginning of the data buffer
   *
   * @param The pointer to the data buffer
   */
  [[shared]] const T* data() const {
    return reinterpret_cast<const T*>(buffer_->data());
  }

 private:
  [[shared]] size_t size_;
  [[shared]] std::shared_ptr<Blob> buffer_;

  friend class Client;
  friend class ArrayBaseBuilder<T>;
};

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}  // namespace vineyard

#endif  // MODULES_BASIC_DS_ARRAY_VINEYARD_MOD_

// vim: syntax=cpp
